package com.honeycomb.action.web.map.ajax;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.honeycomb.action.BaseAjaxAction;
import com.honeycomb.service.IMapService;
import com.honeycomb.utils.SystemConstants;
import com.honeycomb.vo.ArticleinfoVO;
import com.honeycomb.vo.ResultDto;
import com.honeycomb.vo.action.map.AjaxMapForGetArticleRoundPointVO;

import com.honeycomb.utils.ErrorMsgConstants;

/**
 * 
 * @author Wangyu
 * 
 * @function AJAX请求查询指定范围的文章列表
 * 
 * @return json
 * 
 */
public class AjaxMapForGetArticleRoundPointAction extends BaseAjaxAction {

	private IMapService iMapService;

	public IMapService getiMapService() {
		return iMapService;
	}

	public void setiMapService(IMapService iMapService) {
		this.iMapService = iMapService;
	}

	private double x;
	private double y;
	private int distance;

	public double getX() {
		return x;
	}

	public void setX(double x) {
		this.x = x;
	}

	public double getY() {
		return y;
	}

	public void setY(double y) {
		this.y = y;
	}

	public int getDistance() {
		return distance;
	}

	public void setDistance(int distance) {
		this.distance = distance;
	}

	@Override
	protected JSONObject doRequest(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws Exception {
		logger.info("--------------------AjaxAddArticleAction--------------------");
		JSONObject jo = new JSONObject();
		ResultDto dto = null;
		try {
			int nowPage = this.getPageNum();
			dto = this.getiMapService().getArticleinfoVOListByMapPoint(x, y, distance, nowPage);
			AjaxMapForGetArticleRoundPointVO amgarpv = (AjaxMapForGetArticleRoundPointVO)dto.getFirstResult();
			List<ArticleinfoVO> aivl = amgarpv.getAivl();
			int totalCount = amgarpv.getTotleCount();
			jo.put("result", SystemConstants.AJAX_SUCCESS);
				JSONArray ja = new JSONArray();
				for (int i = 0; i < aivl.size(); i++) {
					JSONObject jo1 = new JSONObject();
					ArticleinfoVO aiv = aivl.get(i);
					jo1.put("articleId", aiv.getArticleId());
					jo1.put("articleTitle", aiv.getArticleTitle());
					jo1.put("articleSummary", aiv.getArticleSummary());
					jo1.put("mapX", aiv.getMapInfo().getMapX());
					jo1.put("mapY", aiv.getMapInfo().getMapY());
					ja.add(jo1);
				}
			
			jo.put("list", ja);
			jo.put("totalCount", totalCount);
			jo.put("nowPage", nowPage);
			return jo;
		} catch (Exception e) {
			e.printStackTrace();
			jo.put("result", ErrorMsgConstants.encoderExceptionMessage(e.toString()) );
			jo.put("msg", dto.getErrorMSG());
			return jo;
		}
	}

}
